﻿// -----------------------------------------------------------------------
// <copyright file="DataStorage.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Data
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// Stores data used for data binding (labels and other controls displayed on the forms)
    /// </summary>
    public static class DataStorage
    {
        /// <summary> The table that contains our data </summary>
        private static DataTable table;

        /// <summary> Initializes static members of the DataStorage class. </summary>
        static DataStorage()
        {
            table = new DataTable();
            DataRow row;
            row = table.NewRow();
            table.Rows.Add(row);

            LoadDataTable();

            // This updates some variables
            Updater updater = new Updater();
        }

        /// <summary>
        /// Gets the current table
        /// </summary>
        public static DataTable Table
        {
            get
            {
                return table;
            }
        }

        /// <summary>
        /// Set value
        /// </summary>
        /// <param name="strProperty">String property name</param>
        /// <param name="strValue">String value</param>
        public static void SetProperty(string strProperty, string strValue)
        {
            try
            {
                if (!table.Columns.Contains(strProperty))
                {
                    DataColumn column;
                    column = table.Columns.Add();
                    column.ColumnName = strProperty;
                    column.DataType = typeof(string);
                    table.Rows[0][strProperty] = strValue;
                }
                else
                {
                    table.Rows[0][strProperty] = strValue;
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// Gets specific property
        /// </summary>
        /// <param name="strPropery">The property to get</param>
        /// <returns>Returns string empty if the property does not exist</returns>
        public static string GetProperty(string strPropery)
        {
            if (table.Rows[0][strPropery] != null)
            {
                return table.Rows[0][strPropery].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Load table with values from xml
        /// </summary>
        private static void LoadDataTable()
        {
            // Object to load our xml configuration file
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(Metal.Config.Paths.ConfigFolder + "DataFields.xml");

            XmlNode fields = xmlConfig.SelectSingleNode("Fields");

            foreach (XmlNode field in fields)
            {
                SetProperty(field.Name, field.InnerText);
            }
        }
    }
}
